package cn.wcy.design_patterns.creationtype01.factorymethod01.test;

import cn.wcy.design_patterns.creationtype01.factorymethod01.card.IQiYiCardService;
import cn.wcy.design_patterns.creationtype01.factorymethod01.goods.GoodsService;
import cn.wcy.design_patterns.creationtype01.factorymethod01.coupon.CouponResult;
import cn.wcy.design_patterns.creationtype01.factorymethod01.coupon.CouponService;
import cn.wcy.design_patterns.creationtype01.factorymethod01.goods.DeliverReq;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;

@Slf4j
public class AfterPrizeController {

    public AwardRes awardToUser(AwardReq req) {
        AwardRes apply;
        try {
            Map<Integer, Function<AwardReq, AwardRes>> strategyMap = new ConcurrentHashMap<>(3);
            strategyMap.put(1, this.getCouponAwardRes());
            strategyMap.put(2, this.getMatterAwardRes());
            strategyMap.put(3, this.getThirdPartyAwardRes());
            Function<AwardReq, AwardRes> awardReqAwardResFunction = strategyMap.get(req.getAwardType());
            log.info("奖品发放完成{}。", req.getUId());
            apply = awardReqAwardResFunction.apply(req);
        } catch (Exception e) {
            String reqJson = JSON.toJSONString(req);
            log.error("奖品发放失败{}。req:{}", req.getUId(), reqJson, e);
            return new AwardRes("0001", e.getMessage());
        }
        return apply;
    }

    private Function<AwardReq, AwardRes> getCouponAwardRes() {
        return awardReq -> {
            CouponService couponService = new CouponService();
            CouponResult couponResult = couponService.sendCoupon(awardReq.getUId(), awardReq.getAwardNumber(), awardReq.getBizId());
            if ("0000".equals(couponResult.getCode())) {
                return new AwardRes("0000", "发放成功");
            } else {
                return new AwardRes("0001", couponResult.getInfo());
            }
        };
    }

    private Function<AwardReq, AwardRes> getMatterAwardRes() {
        return awardReq -> {
            GoodsService goodsService = new GoodsService();
            DeliverReq deliverReq = new DeliverReq();
            deliverReq.setUserName(queryUserName(awardReq.getUId()));
            deliverReq.setUserPhone(queryUserPhoneNumber(awardReq.getUId()));
            deliverReq.setSku(awardReq.getAwardNumber());
            deliverReq.setOrderId(awardReq.getBizId());
            deliverReq.setConsigneeUserName(awardReq.getExtMap().get("consigneeUserName"));
            deliverReq.setConsigneeUserPhone(awardReq.getExtMap().get("consigneeUserPhone"));
            deliverReq.setConsigneeUserAddress(awardReq.getExtMap().get("consigneeUserAddress"));
            Boolean isSuccess = goodsService.deliverGoods(deliverReq);
            if (Boolean.TRUE.equals(isSuccess)) {
                return new AwardRes("0000", "发放成功");
            } else {
                return new AwardRes("0001", "发放失败");
            }
        };
    }

    private Function<AwardReq, AwardRes> getThirdPartyAwardRes() {
        return awardReq -> {
            String bindMobileNumber = queryUserPhoneNumber(awardReq.getUId());
            IQiYiCardService iQiYiCardService = new IQiYiCardService();
            iQiYiCardService.grantToken(bindMobileNumber, awardReq.getAwardNumber());
            return new AwardRes("0000", "发放成功");
        };
    }

    private String queryUserName(String uId) {
        return "花花" + uId;
    }

    private String queryUserPhoneNumber(String uId) {
        return "15200101232" + uId;
    }

}
